package com.wyj.utils

import android.content.Context
import android.media.SoundPool

@Synchronized
fun SoundPool.play(context: Context, rawName: String, loop: Int = 0, streamIdCallback: ((Int) -> Unit)? = null) {
    ResourceManager.getRawResId(context, rawName).let {
        if (it > 0) {
            this.play(context, it, loop, streamIdCallback)
        }
    }
}

@Synchronized
fun SoundPool.play(context: Context, rawId: Int, loop: Int = 0, streamIdCallback: ((Int) -> Unit)? = null) {
    val priority = System.currentTimeMillis().toString().let {
        it.substring(it.length - 9, it.length).toInt()
    }
    this.setOnLoadCompleteListener { soundPool, sampleId, status ->
        soundPool.play(sampleId, 1f, 1f, priority, loop, 1f).let {
            streamIdCallback?.invoke(it)
        }
        soundPool.unload(sampleId)
    }
    this.load(context, rawId, priority)
}